<?php 
	require_once('../params.inc');
	require_once('../data.inc');
	require_once('../utils.inc');
	require_once('sf.inc');
	require_once('../shop.inc');
	
	$thisCat = new Category('products');

	$id = $par->get('productId');
	$product = new Product($id);
	
	$path = array(
		array('Главная','index.php'),
		array($thisCat->descr(),'view.php?name=products'),
		"Аттрибуты товара '{$product->name()}'"
	);
	
	require_once('header.inc'); 
	
	$attrs = new ProductAttributes($id, true);
?>

<script lang=javascript>
	function submit() 
	{
		var oF = document.getElementById('oForm');
		if (oF.onsubmit()) {
			oF.submit();
		}
	}
	
	function onOk(oForm) 
	{
		var msg = 'Ошибка! Значение аттрибута не задано или заданно некорректно.';
		if (validateControlsInput(msg)) 
		{
			addCtlValuesAsInputsOf(oForm);
			return true;
		}
		return false;
	}
</script>

<form id=oForm action='do_product.php' method=post onsubmit="return (onOk(this));">
<table width=100%>
	<input type=hidden name=do value="attributes" />
	<?php
		echo "<input type=hidden name=productId value='$id'/>";
		
		$nullAllow = true;
		$readOnly = false;

		foreach ($attrs as $attr) 
		{
			$attrId = 'attribute_' . $attr->id();
			
			if ($attr->isList()) {
				$ctl = new ListEditControl($attrId, $nullAllow, $readOnly);
				foreach ($attr->allowedValues() as $allowedValue) {
					$ctl->addOption($allowedValue[0], $allowedValue[1]);
				}
			}
			else {
				switch ($attr->type()) 
				{
					case AttributeTypes::STRING:
						$ctl = new StringEditControl($attrId, $attr->length(), $nullAllow, $readOnly);
						break;
					case AttributeTypes::INTEGER:
						$ctl = new IntegerEditControl($attrId, $attr->length(), $nullAllow, $readOnly);
						break;
					case AttributeTypes::PRICE:
					case AttributeTypes::FLOAT1:
					case AttributeTypes::FLOAT2:
					case AttributeTypes::FLOAT3:
						$ctl = new FloatEditControl($attrId, $attr->length(), $nullAllow, $readOnly);
						$ctl->setScale($attr->scale());
						break;
					default:
						die("Invalid attribute type [{$attr->type()}]");
				}
			}
			
			$ctl->setValue($attr->value());
			$ctlText = $ctl->render();
			
			echo
				"<tr>" .
					"<td style='width=10px;'></td>" .
					"<td align=right nowrap><b>{$attr->name()}:</b></td>" .
					"<td nowrap width=100%>$ctlText</td>" .
					"<td style='width=10px;'></td>" .
				"</tr>";
		}		
	?>
</table>
</form>


<table width=100%>
	<tr>
		<td width=100% align=right>
			<button onclick="submit()">Сохранить</button>
		</td>
	</tr>
</table>

<script lang=javascript>
<?php
	if ($attrs->is_empty()) {
		echo "submit();";
	}
?>
</script>

<?php require_once('footer.inc'); ?>